package corej.course.net;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class TCPStringServer {
    public static void main(String[] args) throws Exception {
        ServerSocket ss = new ServerSocket(10000);
        int i = 1;
        while (true) {
            Socket s = ss.accept();
            Thread t = new StringThread(s, i++);
            t.start();
        }
    }
}

class StringThread extends Thread {
    Socket s;

    int i;

    public StringThread(Socket s, int i) {
        super();
        this.s = s;
        this.i = i;
    }

    public void run() {
        try {
            OutputStream os = s.getOutputStream();
            PrintWriter out = new PrintWriter(os);
            InputStream is = s.getInputStream();
            BufferedReader in = new BufferedReader(new InputStreamReader(is));

            String text = in.readLine();

            out.println(text.toUpperCase());
            out.flush();

            out.println("You are " + i + " client");
            out.flush();

            s.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
